package com.LachesismStorm.lachesism.ali.oss.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PreDestroy;

/**
 * OssConfig
 *
 * @author peiyu
 * @date 2023/10/26
 */
@Configuration
public class OssConfig {
    /**
     * 将 <your-endpoint> 替换为 Endpoint，例如 oss-cn-hangzhou.aliyuncs.com
     */
    private String endpoint = "oss-cn-shanghai.aliyuncs.com";
    /**
     * 将 <your-bucket> 替换为 Bucket 名称
     */
    private String bucket = "<your-bucket>";
    /**
     * 指定上传到 OSS 的文件前缀
     */
    private String dir = "user-dir-prefix/";
    /**
     * 指定过期时间，单位为秒
     */
    private long expireTime = 3600;
    /**
     * 构造 host
     */
    private String host = "http://" + bucket + "." + endpoint;
    /**
     *  accessKeyId
     */
    private String accessKeyId = "LTAI5tQdczLQY9zYLqUb2UVD";
    /**
     *  accessKeySecret
     */
    private String accessKeySecret = "dxfLTdB6WfFMKnRf8gmCPpHcQht4Yi";

    private OSS ossClient;
    @Bean
    public OSS getOssClient() {
        ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        return ossClient;
    }
    @Bean
    public String getHost() {
        return host;
    }

    @Bean
    public String getEndpoint() {
        return endpoint;
    }
    @Bean
    public String getAccessKeyId() {
        return accessKeyId;
    }
    @Bean
    public long getExpireTime() {
        return expireTime;
    }
    @Bean
    public String getDir() {
        return dir;
    }

    @PreDestroy
    public void onDestroy() {
        ossClient.shutdown();
    }
}
